﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Sensors;
using System.Net.Sockets;
using System.Globalization;

namespace HTCDiamondTouchClient
{
    public partial class MainForm : Form
    {
        private IGSensor _gSensor;
        private TcpClient _tcpClient;

        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            _gSensor = GSensorFactory.CreateGSensor();
            
            timSendAccelerationData.Enabled = true;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            if (_tcpClient != null &&
                _tcpClient.Client.Connected)
            {
                _tcpClient.Close();
            }

            Close();
        }

        private void timSendAccelerationData_Tick(object sender, EventArgs e)
        {
            string deliveryString = "";

            //connect raw sensor data variables with container
            GVector gVector = _gSensor.GetGVector();
            //concatenate each values of sensor data
            deliveryString += "X:" + gVector.X.ToString(CultureInfo.InvariantCulture) + "|";
            deliveryString += "Y:" + gVector.Y.ToString(CultureInfo.InvariantCulture) + "|";
            deliveryString += "Z:" + gVector.Z.ToString(CultureInfo.InvariantCulture) + "|";

            string accelerationState = string.Empty;
            accelerationState = "X: " + gVector.X + Environment.NewLine;
            accelerationState += "Y: " + gVector.Y + Environment.NewLine;
            accelerationState += "Z: " + gVector.Z + Environment.NewLine;
            accelerationState += "Length: " + gVector.Length;
            lblAccelerationState.Text = accelerationState;
            
            SendDataAsBinary(deliveryString);
        }

        private void SendDataAsBinary(string data)
        {
            try
            {
                if (_tcpClient == null ||
                    !_tcpClient.Client.Connected)
                {
                    _tcpClient = new TcpClient("169.254.2.2", 12345);
                }

                //create byte array and convert delivery stream
                Byte[] sendBytes = Encoding.ASCII.GetBytes(data + Environment.NewLine);
                //send bytes to server
                _tcpClient.Client.Send(sendBytes);
            }
            catch (Exception)
            { }
        }


        private void pnlLeftButton_MouseDown(object sender, MouseEventArgs e)
        {
            pnlLeftButton.BackColor = Color.Black;
            SendDataAsBinary("M:1");
        }

        private void pnlLeftButton_MouseUp(object sender, MouseEventArgs e)
        {
            pnlLeftButton.BackColor = SystemColors.Control;
            SendDataAsBinary("M:2");
        }

        private void pnlRightButton_MouseDown(object sender, MouseEventArgs e)
        {
            pnlRightButton.BackColor = Color.Black;
            SendDataAsBinary("M:3");
        }

        private void pnlRightButton_MouseUp(object sender, MouseEventArgs e)
        {
            pnlRightButton.BackColor = SystemColors.Control;
            SendDataAsBinary("M:4");
        }

        private void MainForm_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SendDataAsBinary("G:1");
                e.Handled = true;
            }
        }

        private void MainForm_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SendDataAsBinary("G:2");
                e.Handled = true;
            }
        }
    }
}